import Vue 				from 'vue'
import util 			from '../../util'
import axios 			from 'axios'
import { List,Toast } 	from 'vant'
Vue.use(List,Toast)

export default {
	name: 'orderList',
	data(){
		return {
			pageState:'',		//1:今日交易明细   2:今日入金明细   3:今日出金明细
			pageIndex:1,
			pageSize:20,
			list:[],
			loading:false,
			finished:false,
			loadError:false,
			offset:200,
			date:'',
		}
	},
	computed:{
		topDistance(){
			//容器与顶部距离
			let clientWidth = document.documentElement.clientWidth;
			let _rem = clientWidth >= 750 ? 100 : 100 * (clientWidth / 750);
			return 'top:'+(0.5*_rem+100)+'px';
		},
		showDate(){
			let _arr = this.date.split('-');
			return _arr[1]+'-'+_arr[2]
		}
	},
	methods:{
		load(){
			this.loading = true;
			if(!this.token || !this.pageState){
				setTimeout(()=>{
					this.load();
				},300)
				return
			}
			let _url = this.pageState==1?util.ajaxUrl2+'/api/v1/user/rebate-log':util.ajaxUrl2+'/api/v1/user/money-log';
			let _params = this.pageState==1?{
				page: this.pageIndex,
				size: this.pageSize,
				product_group: '',
				search: '',
				start: this.date,
                end: this.date,
			}:{
				uid: '',
                start: this.date,
                end: this.date,
                type:this.pageState==2?'in':'out',
                page:this.pageIndex,
                size:this.pageSize
			}
			util.fetch2(_url,_params).then(res => {
				this.loading = false;
				if(res.code === 0){
					res.data.list.map(item => {
						if(Object.prototype.hasOwnProperty.call(item,'money')) item.money = util.countNum(item.money,item.currency==='USDT'?2:8);
                        if(Object.prototype.hasOwnProperty.call(item,'fee')) item.fee   = util.countNum(item.fee,item.currency==='USDT'?2:8);
					})
					this.list = this.list.concat(res.data.list);
					if(!res.data || res.data.list && res.data.list.length < this.pageSize){
						this.finished = true;
					}
				}else{
					this.loadError = true;
					Toast(res.retMsg || this.$t('noError'))
				}
			}).catch(error => {
				this.loading = false;
				this.loadError = true;
			}).finally(e => {
				this.pageIndex++;
			})
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		if(this.$route.query.pageState) this.pageState = Number(this.$route.query.pageState);
		if(this.$route.query.date) this.date = this.$route.query.date;
		util.waitSDKReady().then(token => {
			this.token = token;
		}).catch((e) => {
			console.log('获取token失败')
		})
		setTimeout(()=>{
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:this.themeIndex==1?'light':'dark'
			})
		},500)
    }
}